#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<int> weights(5);
    for (int i = 0; i < 5; i++) {
        cin >> weights[i];
    }
    map<string, int> hot;
    for (int i = 0; i < n; i++) {
        string name;
        int watch, star, fork, issue, mr;
        cin >> name >> watch >> star >> fork >> issue >> mr;
        hot[name] = watch * weights[0] + star * weights[1] + fork * weights[2] + issue * weights[3] + mr * weights[4];
    }
    vector<pair<string, int>> hot_vec(hot.begin(), hot.end());
    for(auto &i:hot_vec){
        cout << i.first << " " << i.second << endl;
    }
    sort(hot_vec.begin(), hot_vec.end(), [](pair<string, int> a, pair<string, int> b) {
        if (a.second == b.second) {
            return a.first < b.first;
        }
        return a.second > b.second;
    });
    for (int i = 0; i < n; i++) {
        cout << hot_vec[i].first << endl;
    }
    return 0;
}

/*
#include <bits/stdc++.h>
using namespace std;
 
class Project {
public:
    string name;
    int hot;
 
    Project(string name, int hot) : name(std::move(name)), hot(hot) {}
};
 
int main() {
    int n;
    cin >> n;
 
    int weights[5];
    for (int &weight: weights) {
        cin >> weight;
    }
 
    vector<Project> projects;
    for (int i = 0; i < n; i++) {
        string name;
        cin >> name;
 
        int hot = 0;
        for (int weight: weights) {
            int count;
            cin >> count;
 
            hot += count * weight;
        }
 
        projects.emplace_back(name, hot);
    }
 
    sort(projects.begin(), projects.end(), [](const Project &a, const Project &b) {
        if (a.hot != b.hot) {
            return a.hot > b.hot;
        } else {
            string lowerAName;
            for (const auto &c: a.name) {
                lowerAName.push_back(c);
            }
 
            string lowerBName;
            for (const auto &c: b.name) {
                lowerBName.push_back(c);
            }
 
            return lowerAName < lowerBName;
        }
    });
 
    for (const auto &p: projects) {
        cout << p.name << endl;
    }
 
    return 0;
}*/